﻿using AutoMapper;
using B.Z.BasicData.API.Read.DTO.ExcuseDtos;
using B.Z.BasicData.Domain.Entity.Excused;
using B.Z.BasicData.ErrorCode;
using B.Z.BasicData.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;

namespace B.Z.BasicData.API.Read.Application.Excused
{
    public class ExcusedQueryCommandHandler : IRequestHandler<ExcusedQueryCommand, APIResult<APIPaging<ExcusedDto>>>
    {
        private readonly IBaseRepository<T_Excused> excusedRepository;
        private readonly IBaseRepository<T_Department> departRepository;
        private readonly IBaseRepository<T_ExcusedType> typeRepository;

        public ExcusedQueryCommandHandler(IBaseRepository<T_Excused> excusedRepository,IBaseRepository<T_ExcusedType> typeRepository, IBaseRepository<T_Department> departRepository)
        {
            this.excusedRepository = excusedRepository;
            this.typeRepository = typeRepository;
            this.departRepository = departRepository;
        }

        public Task<APIResult<APIPaging<ExcusedDto>>> Handle(ExcusedQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<ExcusedDto>> result = new APIResult<APIPaging<ExcusedDto>>();
            result.Code = APIEnum.ok;
            result.Message = "获取成功";
            var list = request.RidList.Split(",").Select(x => Convert.ToInt32(x));
            var excuselist = excusedRepository.GetAll().AsNoTracking().Where(x => x.Uid == request.Uid);

            if (list.Contains(11))
            {
                excuselist = excusedRepository.GetAll().AsNoTracking().Where(x => x.DepartId == request.DepartId);
            }
            if (!string.IsNullOrEmpty(request.Name)) 
            {
                excuselist = excuselist.Where(x => x.Name.Contains(request.Name));
            }
            if (request.Start != null && request.End != null) 
            {
                excuselist = excuselist.Where(x => x.StartDateTime >= request.Start&&x.EndDateTime<request.End);
            }
            if (request.Status != null) 
            {
                excuselist = excuselist.Where(x => x.Status==request.Status);
            }
            var dtolist = from a in excuselist
                          join b in departRepository.GetAll()
                          on a.DepartId equals b.Id
                          join c in typeRepository.GetAll()
                          on a.ExcusedType equals c.Id
                          select new ExcusedDto
                          {
                              Id = a.Id,
                              Name = a.Name,
                              DepartId = a.DepartId,
                              ExcusedType = a.ExcusedType,
                              StartDateTime = a.StartDateTime,
                              EndDateTime = a.EndDateTime,
                              TotalHour = a.TotalHour,
                              BackDateTime = a.BackDateTime,
                              Reason = a.Reason,
                              LeaderName = a.LeaderName,
                              Status = a.Status,
                              Remark = a.Remark,
                              WhyRefuse = a.WhyRefuse,
                              TypeName = c.TypeName,
                              DepartName = b.DepartName,
                              CreateTime=a.CreateTime,
                              Uid=a.Uid,
                          };
              APIPaging < ExcusedDto > paging = new APIPaging<ExcusedDto>();
            paging.RecordCount = dtolist.Count();
            paging.List = dtolist.OrderByDescending(x=>x.Id).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
            result.Data = paging;
            return Task.FromResult(result);



        }
    }
}
